package com.itheima.web.controller.pms;


import com.itheima.service.pms.PmsBrandService;
import com.itheima.utils.ResultData;
import com.itheima.vo.pms.Brand;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
@Slf4j
public class PmsBrandController {

    @DubboReference
    private PmsBrandService pmsBrandService;

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping("/create")
    public ResultData saveBrand (@RequestBody Brand brand){
        log.info("brand:{}", brand);
        return pmsBrandService.insert(brand);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public ResultData batch(@RequestParam List<Integer> ids){
        log.info("ids:{}",ids);
        return pmsBrandService.removeIds(ids);
    }


    /**
     * 删除品牌
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResultData delete(@PathVariable  Integer id ){
        log.info("deleteId:{}",id);
        pmsBrandService.removeById(id);
        return ResultData.ok("删除成功","ok");
    }


    /**
     * 根据品牌名称分页获取品牌列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResultData list (String keyword , Integer pageNum , Integer pageSize){
        return pmsBrandService.listPage(keyword , pageNum , pageSize);
    }

     /*
     获取全部品牌列表
     */
    @GetMapping("/listAll")
    public ResultData listAll ( ){
        return ResultData.ok("OK" , pmsBrandService.list());
    }

    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @param factoryStatus
     * @return
     */
    @PostMapping("update/factoryStatus")
    public ResultData factoryStatus(@RequestParam List<Integer> ids,Integer factoryStatus){
        pmsBrandService.updateFactoryStatus(ids,factoryStatus);
        return ResultData.ok("操作成功",ids.size());
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("update/showStatus")
    public ResultData showStatus(@RequestParam List<Integer> ids,Integer showStatus){
        pmsBrandService.updateShowStatus( ids , showStatus);
        return ResultData.ok("操作成功",ids.size());
    }

    /**
     *更新品牌
     * @param id
     * @param brand
     * @return
     */
    @PostMapping("update/{id}")
    public ResultData updateById(@PathVariable Integer id, @RequestBody Brand brand){
        brand.setId(id);
        pmsBrandService.updateById(brand);
        return ResultData.ok("操作成功","");
    }

    /**
     *根据编号查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResultData getById(@PathVariable Integer id){
        return ResultData.ok("操作成功", pmsBrandService.getById(id));
    }
}
